//
//  RJPhotoViewController.m
//  Muses
//
//  Created by hzyd-wlj on 14/11/17.
//  Copyright (c) 2014年 yodo. All rights reserved.
//

#import "RJPhotoViewController.h"
#import "RJPhotoImageView.h"
#import "RJPhoto.h"

@interface RJPhotoViewController ()<RJPhotoViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIView *captionView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITextView *content_TxtView;
@property (weak, nonatomic) IBOutlet UILabel *pageIndex_lbl;
@property (weak, nonatomic) IBOutlet UIToolbar *bottomToolBar;

/**
 *  评论数量Label
 */
@property (strong, nonatomic) UILabel *commentCount_Lbl;
@property (strong, nonatomic, readwrite) RJPhotoImageView *currentZoomedImageView;

@property (strong, nonatomic) NSMutableArray *photoImageViews;
@end

@implementation RJPhotoViewController

#pragma mark - View Life cycle

- (instancetype)initWithDataSource:(id<RJPhotoViewControllerDataSource>)dataSource delegate:(id<RJPhotoViewControllerDelegate>)delegate
{
    self = [super init];
    if (self) {
        _dataSourse = dataSource;
        _delegate = delegate;
        _photoImageViews = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setupNavbar];
    self.automaticallyAdjustsScrollViewInsets = NO;
    NSInteger dataCount = [self.dataSourse photoViewNumberOfPhoto];
    [self _setPageIndex:1];
    self.scrollView.contentSize = CGSizeMake(AppBounds.size.width*dataCount, AppBounds.size.height);
    
    for (int i = 0;i < dataCount;i++) {
        RJPhotoImageView *photoImageView = [[RJPhotoImageView alloc] initWithFrame:CGRectMake(i*AppBounds.size.width, 0, AppBounds.size.width, AppBounds.size.height)];
        photoImageView.delegate = self;
        NSAssert([self.dataSourse photoView:self photoForRow:i], @"RJPhotoViewController must return a valid photo");
       
        RJPhoto *photo = [self.dataSourse photoView:self photoForRow:i];
        photoImageView.photo = photo;
        [self.scrollView addSubview:photoImageView];
        
        if (i == 0) {
            //如果每个Photo都含有单独的Title 和 Caption的话，就删除这个判断
            self.titleLabel.text = photo.title;
            self.content_TxtView.text = photo.caption;
            
            self.currentZoomedImageView = photoImageView;
        }
        [self.photoImageViews addObject:photoImageView];
    }
}

#pragma mark - Setups
- (void)setupNavbar
{
    [self addBackButton];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setFrame:CGRectMake(0, 0, 80, 40)];
    [rightButton setImage:[UIImage imageNamed:@"动态评论"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightBar_Touched:) forControlEvents:UIControlEventTouchUpInside];
    rightButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.commentCount_Lbl = ({
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 0, 40, 40)];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor redColor];
        label.font = [UIFont systemFontOfSize:14];
        label.textAlignment = NSTextAlignmentCenter;
        label.adjustsFontSizeToFitWidth = YES;
        label;
    });
    
    [rightButton addSubview:self.commentCount_Lbl];
    
    self.commentCount_Lbl.text = [NSString stringWithFormat:@"（%ld）",[self.delegate numberOfCommentCount]];
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];

    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}

- (void)rightBar_Touched:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(photoViewDidSelectCommentButton)]) {
        [self.delegate photoViewDidSelectCommentButton];
    }
}

#pragma mark - RJPhotoImageView Delegate
- (void)photoViewDidSingleTap:(RJPhotoImageView *)photoView
{
    [self toggleFullScreen];
}

- (void)toggleFullScreen {
    
    _isFullScreen = !_isFullScreen;
    
    if (!_isFullScreen) {
        // fade in navigation
        [UIView animateWithDuration:0.3 animations:^{
            [self setNeedsStatusBarAppearanceUpdate];
            self.navigationController.navigationBar.alpha = 1.0;
            self.navigationController.toolbar.alpha = 1.0;
            self.captionView.alpha = 1.0;
        } completion:^(BOOL finished) {
            
        }];
    }else {
        // fade out navigation
        [UIView animateWithDuration:0.3 animations:^{
            [self setNeedsStatusBarAppearanceUpdate];
            self.navigationController.navigationBar.alpha = 0.0;
            self.navigationController.toolbar.alpha = 0.0;
            self.captionView.alpha = 0.0;
        } completion:^(BOOL finished) {

        }];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    RJPhotoImageView *photoImageView = self.photoImageViews[_currentIndex];
    if (photoImageView.isScaleChanged) {
        [photoImageView resetImageViewScale:1 Center:photoImageView.center];
    }
    
    _currentIndex = scrollView.contentOffset.x/CGRectGetWidth(scrollView.frame);
    
    [self _setPageIndex:_currentIndex + 1];
 
    
    
    //TODO:如果每个照片都含有自己的 Title 和 Content，则去除下面注释
//    RJPhoto *photo = [self.dataSourse photoView:self photoForRow:_currentIndex];
//    self.titleLabel.text = photo.title;
//    self.content_TxtView.text = photo.caption;
}

- (void)_setPageIndex:(NSInteger)currentIndex
{
    NSInteger totalCount = [self.dataSourse photoViewNumberOfPhoto];
    if (totalCount > 1) {
        self.pageIndex_lbl.text = [NSString stringWithFormat:@"%ld/%ld",currentIndex,totalCount];
    }else{
        self.pageIndex_lbl.text = @"";
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
